package com.xmy.servlet;

import com.xmy.entity.User;
import com.xmy.service.UserService;
import com.xmy.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 注册
 */
public class ResgistServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String method = request.getParameter("method");
        UserService us = new UserServiceImpl();
        if (method.equals("regist")){//注册
            User user = new User();
            user.setUname(request.getParameter("username"));
            user.setPwd(request.getParameter("password"));
            user.setPhone(request.getParameter("tel"));
            user.setNickName(request.getParameter("nc"));
            us.registUser(user);
            response.sendRedirect("index.jsp");
        }else if(method.equals("regName")){//查询用户名是否重复
            String name = request.getParameter("name");
            int i = us.selCountByName(name);
            out.print(i);
            out.flush();
            out.close();
        }
    }
}
